#include "CC1101.h"

// Address Config = No address check
// Base Frequency = 399.999939
// CRC Autoflush = false
// CRC Enable = true
// Carrier Frequency = 429.992615
// Channel Number = 150
// Channel Spacing = 199.951172
// Data Format = Normal mode
// Data Rate = 2.00224
// Deviation = 5.157471
// Device Address = 0
// Manchester Enable = false
// Modulated = true
// Modulation Format = GFSK
// PA Ramping = false
// Packet Length = 255
// Packet Length Mode = Variable packet length mode. Packet length configured by the first byte after sync word
// Preamble Count = 4
// RX Filter BW = 58.035714
// Sync Word Qualifier Mode = 30/32 sync word bits detected
// TX Power = -30
// Whitening = false
const uint8_t PaTabel[ ] = PA_TABLE;
static const uint8_t CC1101InitData[ 40 ][ 2 ] =
{
    {CC1101_IOCFG0,      0x06},
    {CC1101_FIFOTHR,     0x47},
    {CC1101_PKTCTRL0,    0x05},
    {CC1101_CHANNR,      0x96},
    {CC1101_FSCTRL1,     0x06},
    {CC1101_FREQ2,       0x0F},
    {CC1101_FREQ1,       0x62},
    {CC1101_FREQ0,       0x76},
    {CC1101_MDMCFG4,     0xF6},
    {CC1101_MDMCFG3,     0x43},
    {CC1101_MDMCFG2,     0x13},
    {CC1101_DEVIATN,     0x15},
    {CC1101_MCSM0,       0x18},
    {CC1101_FOCCFG,      0x16},
    {CC1101_WORCTRL,     0xFB},
    {CC1101_FSCAL3,      0xE9},
    {CC1101_FSCAL2,      0x2A},
    {CC1101_FSCAL1,      0x00},
    {CC1101_FSCAL0,      0x1F},
    {CC1101_TEST2,       0x81},
    {CC1101_TEST1,       0x35},
    {0xFF, 0xFF}
};

//写命令
void CC1101_Write_Cmd( uint8_t Command )
{
    NSS_LOW;
    HAL_SPI_Transmit(&hspi1, &Command, 1, 0xFF);
    NSS_HIGH;
}

//CC1101写一个寄存器
void CC1101_Write_Reg( uint8_t Addr, uint8_t WriteValue )
{
    NSS_LOW;
    HAL_SPI_Transmit(&hspi1, &Addr, 1, 0xFF);
    HAL_SPI_Transmit(&hspi1, &WriteValue, 1, 0xFF);
    NSS_HIGH;
}

//CC1101连续写多个寄存器
void CC1101_Write_Multi_Reg( uint8_t Addr, uint8_t *pWriteBuff, uint8_t WriteSize )
{
    NSS_LOW;
    Addr |= WRITE_BURST;
    HAL_SPI_Transmit(&hspi1, &Addr, 1, 0xFF);
    HAL_SPI_Transmit(&hspi1, pWriteBuff, WriteSize, 0xFF);
    NSS_HIGH;
}

//CC1101读一个寄存器
uint8_t CC1101_Read_Reg( uint8_t Addr )
{
    uint8_t l_RegValue = 0;
    NSS_LOW;
    Addr |= READ_SINGLE;
    HAL_SPI_Transmit(&hspi1, &Addr, 1, 0xFF);
    HAL_SPI_Receive(&hspi1, &l_RegValue, 1, 0xFF);
    NSS_HIGH;
    return l_RegValue;
}

//CC1101连续读多个寄存器
void CC1101_Read_Multi_Reg( uint8_t Addr, uint8_t *pReadBuff, uint8_t ReadSize )
{
    NSS_LOW;
    Addr |= READ_BURST;
    HAL_SPI_Transmit(&hspi1, &Addr, 1, 0xFF);
    HAL_SPI_Receive(&hspi1, pReadBuff, ReadSize, 0xFF);
    NSS_HIGH;
}

//CC1101读状态寄存器，所有状态寄存器都是只读的，地址与命令共用，通过BURST来区分
uint8_t CC1101_Read_Status( uint8_t Addr )
{
    uint8_t l_RegStatus = 0;
    NSS_LOW;
    Addr |= READ_BURST;
    HAL_SPI_Transmit(&hspi1, &Addr, 1, 0xFF);
    HAL_SPI_Receive(&hspi1, &l_RegStatus, 1, 0xFF);
    NSS_HIGH;
    return l_RegStatus;
}

//设置地址和地址模式
void CC1101_Set_Address_Mode(uint8_t AddressMode)
{
    uint8_t btmp;
    btmp = CC1101_Read_Reg( CC1101_PKTCTRL1 ) & ( ~0x03 );  //读出,并清空低2位
    btmp |= ( AddressMode & 0x03 );                         //设置参数到低2位
    CC1101_Write_Reg( CC1101_PKTCTRL1, btmp);               //写入寄存器
}

//CC1101初始化WOR功能
void C1101_WOR_Init( void )
{
    CC1101_Write_Reg(CC1101_MCSM0, 0x18);
    CC1101_Write_Reg(CC1101_WORCTRL, 0x78);
    CC1101_Write_Reg(CC1101_MCSM2, 0x00);
    CC1101_Write_Reg(CC1101_WOREVT1, 0x8C);
    CC1101_Write_Reg(CC1101_WOREVT0, 0xA0);
    CC1101_Write_Cmd( CC1101_SWORRST );
}

//CC1101复位
void CC1101_Reset( void )
{
    NSS_HIGH;
    NSS_LOW;
    NSS_HIGH;
    HAL_Delay( 1 );
    CC1101_Write_Cmd( CC1101_SRES );
}

//CC1101初始化
void CC1101_Init( void )
{
    uint8_t i = 0;
    CC1101_Reset( );
    HAL_Delay(10);
    //写入配置参数
    for( i = 0; i < 30; i++ )
    {
        if(CC1101InitData[i][0] != 0xFF)
        {
            CC1101_Write_Reg( CC1101InitData[i][0], CC1101InitData[i][1] );
        }
        else
        {
            break;
        }
    }
    //写入PA Table
    CC1101_Write_Multi_Reg( CC1101_PATABLE, (uint8_t*)PaTabel, 8 );
}

//发送包
void CC1101_Tx_Packet(uint8_t TxSize, uint8_t *pTxBuff )
{
    CC1101_Clear_TxBuffer;
    CC1101_Write_Reg( CC1101_TXFIFO, TxSize);                   //写入长度
    CC1101_Write_Multi_Reg( CC1101_TXFIFO, pTxBuff, TxSize );   //写入数据
    CC1101_Tx_Mode;                                             //发送模式
}
